home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / By the Book / Mac C Primer V2 / 6.1 - MyStarter / CStarterApp.c < prev    next >
Text File  |  1991-08-23  |  8KB  |  280 lines

  1. /************************************************************/
  2. /*                                                            */
  3. /*    CStarterApp Code from Chapter Six of                    */
  4. /*                                                            */
  5. /*        *** The Macintosh Programming Primer ***            */
  6. /*                                                            */
  7. /*    Copyright 1990, Dave Mark                                */
  8. /*                                                            */
  9. /*    This program demonstrates specific Mac programming        */
  10. /*    techniques.                                                */
  11. /*                                                            */
  12. /************************************************************/
  13.  
  14. /*****
  15.  * CStarterApp.c
  16.  *
  17.  *    Application methods for a typical application.
  18.  *
  19.  *  Copyright © 1990 Symantec Corporation.  All rights reserved.
  20.  *
  21.  *****/
  22.  
  23. #include "CStarterApp.h"
  24. #include "CStarterDoc.h"
  25.  
  26. extern    OSType    gSignature;
  27.  
  28. /***
  29.  * IStarterApp
  30.  *
  31.  *    Initialize the application. Your initialization method should
  32.  *    at least call the inherited method. If your application class
  33.  *    defines its own instance variables or global variables, this
  34.  *    is a good place to initialize them.
  35.  *
  36.  ***/
  37.  
  38. void CStarterApp::IStarterApp(void)
  39.  
  40. {
  41.     /*CApplication::IApplication(4, 20480L, 2048L);OLD*/
  42.     CApplication::IApplication(4, 20480L, 20480L, 20480L);
  43.     
  44.     /* Altered by TCL Demo Weaver 1.0 (2/21/90) */
  45.  
  46. /*  The parameters to IApplication are the number of times to call  
  47.     MoreMasters, the number of bytes of heap space to reserve for                       
  48.     monitoring low memory situations, and the credit limit for memory           
  49.     requests.
  50.     
  51.     Four (4) is a reasonable number of MoreMasters calls,                           
  52.     but you should determine a good number for your application                     
  53.     by observing the heap using Lightsbug,                                              
  54.     TMON, or Macsbug. Set this parameter to zero, give your                         
  55.     program a rigorous work-out, then look at the heap and count                        
  56.     how many master pointer blocks have been allocated. Master                      
  57.     pointer blocks are nonrelocatable and have a size of $100                           
  58.     (hex). You should call MoreMasters at least this many                               
  59.     times -- add a few extra just to be safe. The purpose of all                        
  60.     this preflighting is to prevent heap fragmentation. You                             
  61.     don't want the Memory Manager to call MoreMasters and                           
  62.     create a nonrelocatable block in the middle of your heap. By                        
  63.     calling MoreMasters at the very beginning of the program,                           
  64.     you ensure that these blocks are allocated in a group at the                        
  65.     bottom of the heap. 
  66.                                                                         
  67.     The memory reserve is a safeguard for handling low memory                   
  68.     conditions and is used by the GrowMemory method in                              
  69.     CApplication (check there for more comments). In general,                           
  70.     your program should never request a memory block greater                        
  71.     than this reserve size without explicitly checking in                               
  72.     advance whether there is enough free memory to satisfy the                      
  73.     the request.
  74.                                                                                     
  75.     The credit limit specifies a cut-off level for memory                               
  76.     requests which can tap the memory reserve. Requests larger                      
  77.     than this size will not use the memory reserve when the                         
  78.     system pleads for more memory.                          
  79. */
  80.  
  81. }
  82.  
  83.  
  84.  
  85. /***
  86.  * SetUpFileParameters
  87.  *
  88.  *    In this routine, you specify the kinds of files your
  89.  *    application opens.
  90.  *
  91.  *
  92.  ***/
  93.  
  94. void CStarterApp::SetUpFileParameters(void)
  95.  
  96. {
  97.     inherited::SetUpFileParameters();    /* Be sure to call the default method */
  98.  
  99.         /**
  100.          **    sfNumTypes is the number of file types
  101.          **    your application knows about.
  102.          **    sfFileTypes[] is an array of file types.
  103.          **    You can define up to 4 file types in
  104.          **    sfFileTypes[].
  105.          **
  106.          **/
  107.  
  108.     sfNumTypes = 1;
  109.     sfFileTypes[0] = 'TEXT';
  110.  
  111.         /**
  112.          **    Although it's not an instance variable,
  113.          **    this method is a good place to set the
  114.          **    gSignature global variable. Set this global
  115.          **    to your application's signature. You'll use it
  116.          **    to create a file (see CFile::CreateNew()).
  117.          **
  118.          **/
  119.  
  120.     gSignature = '????';
  121. }
  122.  
  123.  
  124. /* Altered by TCL Demo Weaver 1.0 (2/21/90) */
  125.  
  126. /***
  127.  * SetUpMenus 
  128.  *
  129.  * Set up menus which must be created at run time, such as a
  130.  * Font menu. You can eliminate this method if your application
  131.  * does not have any such menus.
  132.  *
  133. ***/
  134.  
  135.  void CStarterApp::SetUpMenus()
  136.  {
  137.  
  138.   inherited::SetUpMenus();  /*  Superclass takes care of adding     
  139.                                 menus specified in a MBAR id = 1    
  140.                                 resource    
  141.                             */                          
  142.  
  143.         /* Add your code for creating run-time menus here */    
  144.  }
  145.  
  146.  
  147.  
  148. /***
  149.  * DoCommand
  150.  *
  151.  *    Your application will probably handle its own commands.
  152.  *    Remember, the command numbers from 1-1023 are reserved.
  153.  *  The file Commands.h contains all the predefined TCL
  154.  *  commands.
  155.  *
  156.  *    Be sure to call the default method, so you can get
  157.  *    the default behvior for standard commands.
  158.  *
  159.  ***/
  160. void CStarterApp::DoCommand(long theCommand)
  161.  
  162. {
  163.     switch (theCommand) {
  164.     
  165.         /* Your commands go here */
  166.     
  167.         default:    inherited::DoCommand(theCommand);
  168.                     break;
  169.     }
  170. }
  171.  
  172.  
  173. /* Altered by TCL Demo Weaver 1.0 (2/21/90) */
  174.  
  175. /***
  176.  *
  177.  * UpdateMenus 
  178.  *
  179.  *   Perform menu management tasks
  180.  *
  181. ***/
  182.  
  183.  void CStarterApp::UpdateMenus()
  184.  {
  185.   inherited::UpdateMenus();     /* Enable standard commands */      
  186.  
  187.     /* Enable the commands handled by your Application class */ 
  188.  }
  189.  
  190.  
  191. /***
  192.  * Exit
  193.  *
  194.  *    Chances are you won't need this method.
  195.  *    This is the last chance your application gets to clean up
  196.  *  things like temporary files before terminating.
  197.  *
  198.  ***/
  199.  
  200. void CStarterApp::Exit()
  201.  
  202. {
  203.     /* your exit handler here */
  204. }
  205.  
  206.  
  207. /***
  208.  * CreateDocument
  209.  *
  210.  *    The user chose New from the File menu.
  211.  *    In this method, you need to create a document and send it
  212.  *    a NewFile() message.
  213.  *
  214.  ***/
  215.  
  216. void CStarterApp::CreateDocument()
  217.  
  218. {
  219.     CStarterDoc    *theDocument;
  220.     
  221.     theDocument = new(CStarterDoc);
  222.         
  223.         /**
  224.          **    Send your document an initialization
  225.          **    message. The first argument is the
  226.          **    supervisor (the application). The second
  227.          **    argument is TRUE if the document is printable.
  228.          **
  229.          **/
  230.     
  231.     theDocument->IStarterDoc(this, TRUE);
  232.  
  233.         /**
  234.          **    Send the document a NewFile() message.
  235.          **    The document will open a window, and
  236.          **    set up the heart of the application.
  237.          **
  238.          **/
  239.     theDocument->NewFile();
  240. }
  241.  
  242. /***
  243.  * OpenDocument
  244.  *
  245.  *    The user chose Open… from the File menu.
  246.  *    In this method you need to create a document
  247.  *    and send it an OpenFile() message.
  248.  *
  249.  *    The macSFReply is a good SFReply record that contains
  250.  *    the name and vRefNum of the file the user chose to
  251.  *    open.
  252.  *
  253.  ***/
  254.  
  255. void CStarterApp::OpenDocument(SFReply *macSFReply)
  256.  
  257. {
  258.     CStarterDoc    *theDocument;
  259.     
  260.     theDocument = new(CStarterDoc);
  261.         
  262.         /**
  263.          **    Send your document an initialization
  264.          **    message. The first argument is the
  265.          **    supervisor (the application). The second
  266.          **    argument is TRUE if the document is printable.
  267.          **
  268.          **/
  269.     
  270.     theDocument->IStarterDoc(this, TRUE);
  271.  
  272.         /**
  273.          **    Send the document an OpenFile() message.
  274.          **    The document will open a window, open
  275.          **    the file specified in the macSFReply record,
  276.          **    and display it in its window.
  277.          **
  278.          **/
  279.     theDocument->OpenFile(macSFReply);
  280. }